package com.aigou.compont;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.aigou.pojo.Order;
import com.aigou.service.GoodsService;
import com.aigou.service.IOrderService;

@Component
public class SchedulingService2 {

	@Autowired
	IOrderService orderService;
	@Autowired
	GoodsService goodsService;

	//@Scheduled(cron = "0/10 * * * * ?")
	public void updateStatus() {
		
		// 查询状态等于未支付的
		QueryWrapper<Order> query = new QueryWrapper<>();
		query.eq("pay_status", 0);
		List<Order> list = orderService.list(query);
		System.out.println(list);
		
		/*
		 * 1 11:47:10
		 * 2 11:47:20
		 * 3 11:47:30
		 */
		// 判断是否超时 1 分钟
		for (Order order : list) {
			// 11:48:00
			
			LocalDateTime now = LocalDateTime.now();
			// 11:48:00 < 11:47:10 + 1分钟
			if (now.isAfter(order.getCreateTime().plusMinutes(1l))) {
				// 修改状态
				order.setStatus(3);
				order.setPayStatus(2);
				// update(order);
				// 更新订单状态
				orderService.updateById(order);
				// 回退商品数量
				// TODO 修改定时任务
				//goodsService.addKuCun(order.getGid(), order.getNum());
				// 回退优惠金额
			}
		}
		
	}

}
